# -*- coding: utf-8 -*-
import scrapy
import pymysql
from XCFScrapy.items import foodCategoryItem
from XCFScrapy.items import foodsItem
from XCFScrapy import settings

class CategoryspiderSpider(scrapy.Spider):
    name = 'Categoryspider'
    allowed_domains = ['xiachufang.com']
    start_urls = ['http://xiachufang.com/']

    def parse(self, response):
        # 到数据库获取所有的的食材数据
        foodmaterials_list = self.categroylocal_db()
        for item in foodmaterials_list:
            yield scrapy.Request(item['url'], meta={'item': item, 'page': 0},callback=self.requestCategoryWithData)

    """
    获取分类下对应的菜谱
    """
    def requestCategoryWithData(self, response):
        item = response.meta['item']
        starturl = item['url'] + '?page='
        page = response.meta['page']

        if page < 5:
            page = page + 1
        foods_pages = response.xpath("//div[@class='recipe recipe-215-horizontal pure-g image-link display-block']")
        for food in foods_pages:
            # 菜谱ID
            foodId = item['categoryId']
            # 菜谱详情页
            foodDetailurl = food.xpath("./div/p[@class='name']/a/attribute::href").extract_first("")
            # 菜谱名称
            name = food.xpath("./div/p[@class='name']/a/text()").extract_first("").replace(" ", "").strip()
            # 菜谱图片
            img = food.xpath("./a[1]/div/img/attribute::data-src").extract_first("")
            # 菜谱食材
            materialspan = food.xpath("./div/p[@class='ing ellipsis']/span/text()").extract()
            # 菜谱食材
            materiala = food.xpath("./div/p[@class='ing ellipsis']/a/text()").extract()
            # 菜谱评价
            #stats = food.xpath("./div/p[@class='stats']/span[1]/text()").extract_first("")
            # 做个这个菜谱的人数
            number = food.xpath("./div/p[@class='stats']/span[2]/text()").extract_first("")
            # 菜谱作者
            author = food.xpath("./div/p[@class='author']/a/text()").extract_first("")
            materials = materialspan + materiala

            foodItem = foodsItem()

            foodItem['foodId'] = foodId
            foodItem['name'] = name
            foodItem['img'] = img
            foodItem['stats'] = "9.0"
            foodItem['number'] = number
            foodItem['author'] = author
            foodItem['material'] = ",".join(materials)
            foodItem['foodDetailurl'] = "http://www.xiachufang.com" + foodDetailurl
            yield foodItem

        yield  scrapy.Request(starturl + str(page),meta={'item':item,'page':page},callback=self.requestCategoryWithData)

    """
    获取本地存储的食材分类数据
    """
    def categroylocal_db(self):

        # 打开数据库连接
        db = pymysql.connect(
            host=settings.MYSQL_HOST,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            charset='utf8',
            use_unicode=True)
        # 通过cursor执行增删查改
        cursor = db.cursor()

        # sql 查询语句
        sql = "select * from xiaChuFang_activitycategroy "

        try:
            # 执行sql语句
            cursor.execute(sql)
            # 获取所有记录
            results = cursor.fetchall()
            foodcagroy_list = []
            for row in results:
                item = foodCategoryItem()
                item['title'] = row[0]
                item['url'] = row[1]
                item['categoryId'] = row[2]
                foodcagroy_list.append(item)

            return foodcagroy_list
        except:
            print("Error: unable to fetch data")